package dfbz.com.dao;

import dfbz.com.dao.base.BaseDao;
import dfbz.com.model.Dept;
import dfbz.com.utils.JDBCUtil;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.*;

public class DeptDao extends BaseDao {

    private QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
    private BasicRowProcessor bp = new BasicRowProcessor(new GenerousBeanProcessor());

    public List findDepartments() {
//[{dept=dd,count=??,{people_name=??,..}},]
        //[{id=1, name=研发部}, {id=2, name=推广部}, {id=3, name=行政部},
//        封装又难取出又难

        String sql = "select d.*,i.real_name\n" +
                "from dept d\n" +
                "left join user u on d.id=u.dept_id\n" +
                "left join user_info i on u.id = i.user_id;\n";
        try {
            List<Map<String, Object>> query = qr.query(sql, new MapListHandler());
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Dept> findAllDept() {

        List list = addAll(Dept.class);
        return list;

    }
}
